const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // commentTabIndex:0,
    plTabInex:3,
    type:'',
    taskid:'',
    workid:'',
    tdscoreindex: 5,
    zlscoreindex: 5,
    sdscoreindex: 5,
    avatar:'',
    title:'',
    commentVal:'',
    employ_id:'',
    order_id:'',
    evaluationType: '',//evaluationType==1是雇佣评论，为空是任务评论,2是作品评论
    autoObj:{
      minHeight: 72
    },
  },
  // 切换
  // commentTab: function (e) {
  //   var { index } = e.target.dataset
  //   this.setData({
  //     commentTabIndex: index
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      type: options.type,
      taskid: options.taskid,
      workid: options.workid,
      avatar: options.avatar,
      title: options.title,
    })
    if (options.employ_id){
      this.setData({
        employ_id: options.employ_id,
        evaluationType: options.evaluationType
      })
    }
    if (options.order_id) {
      this.setData({
        order_id: options.order_id,
        evaluationType: options.evaluationType
      })
    }
  },

  // 好评类型
  evaluationTab:function(e){
    this.setData({
      plTabInex: e.currentTarget.dataset.pl
    })
  },
  /*// 总户评价
  scoreImg:function(e){
    if (e.currentTarget.dataset.score == 1){
      this.setData({
        tdscoreindex: e.currentTarget.dataset.scoreindex
      })
    } else if (e.currentTarget.dataset.score == 2){
      this.setData({
        zlscoreindex: e.currentTarget.dataset.scoreindex
      })
    }else{
      this.setData({
        sdscoreindex: e.currentTarget.dataset.scoreindex
      })
    }
  },*/

  tdScoreChange(event){
    this.setData({
      tdscoreindex: event.detail
    })
  },

  zlScoreChange(event){
    this.setData({
      zlscoreindex: event.detail
    })
  },

  sdScoreChange(event){
    this.setData({
      sdscoreindex: event.detail
    })
  },

  // 说明
  commentInp:function(e){
    this.setData({
      commentVal:e.detail.value
    })
  },
  // 提交评价
  submitBtn:function(){
    var _this = this
    var { evaluationType,type, taskid, workid, plTabInex, tdscoreindex, zlscoreindex, sdscoreindex, commentVal } = this.data
    if (commentVal == ''){
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (type == 1) {
      tdscoreindex = ''
    }
    if (plTabInex == 3) {
      plTabInex = 1
    } else if (plTabInex == 1) {
      plTabInex = 3
    }
    // evaluationType==''任务评价
    if (evaluationType == ''){
      var url = app.globalData.baseUrl + 'work/evaluate';
      var data = {
        task_id: taskid,
        comment: commentVal,
        speed_score: sdscoreindex,
        quality_score: zlscoreindex,
        attitude_score: tdscoreindex,
        type: plTabInex,
        work_id: workid,
        token: app.globalData.token
      }
    } else if (evaluationType == 1){
      // evaluationType==1雇佣评价
      var { employ_id } = this.data
      var url = app.globalData.baseUrl + 'user/employEvaluate';
      var data = {
        token: app.globalData.token,
        employ_id: employ_id,
        comment: commentVal,
        type: plTabInex,
        speed_score: sdscoreindex,
        quality_score: zlscoreindex,
        attitude_score: tdscoreindex
      }
    } else if (evaluationType == 2){
      var { order_id } = this.data
      var url = app.globalData.baseUrl + 'user/commentGoods';
      var data = {
        token: app.globalData.token,
        order_id: order_id,
        desc: commentVal,
        type: plTabInex,
        speed_score: sdscoreindex,
        quality_score: zlscoreindex,
        attitude_score: tdscoreindex
      }
    }
    wx.showLoading({
      title: '加载中'
    })
    app.wxPostRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          const  pages=getCurrentPages();
          const beforepage=pages[pages.length-2]
          wx.navigateBack({
            delta: 1,
            success:function (){
              beforepage.orderDatail()
            }
          })
        }, 2000)
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
